package org.dromara.manu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mqtt.client.EmqClient;
import org.dromara.common.mqtt.enums.QosEnum;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.manu.domain.vo.ScreenVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.dromara.manu.domain.bo.ManuPubToolScreenBo;
import org.dromara.manu.domain.vo.ManuPubToolScreenVo;
import org.dromara.manu.domain.ManuPubToolScreen;
import org.dromara.manu.mapper.ManuPubToolScreenMapper;
import org.dromara.manu.service.IManuPubToolScreenService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 车间屏幕管理单Service业务层处理
 *
 * @author Bin Liu
 * @date 2024-07-08
 */
@RequiredArgsConstructor
@Service
public class ManuPubToolScreenServiceImpl implements IManuPubToolScreenService {

    private final ManuPubToolScreenMapper baseMapper;

    @Autowired
    private ISysOssService ossService;

    @Autowired
    private EmqClient emqClient = new EmqClient();

    /**
     * 查询车间屏幕管理单
     *
     * @param id 主键
     * @return 车间屏幕管理单
     */
    @Override
    public ManuPubToolScreenVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询车间屏幕管理单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 车间屏幕管理单分页列表
     */
    @Override
    public TableDataInfo<ManuPubToolScreenVo> queryPageList(ManuPubToolScreenBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ManuPubToolScreen> lqw = buildQueryWrapper(bo);
        Page<ManuPubToolScreenVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的车间屏幕管理单列表
     *
     * @param bo 查询条件
     * @return 车间屏幕管理单列表
     */
    @Override
    public List<ManuPubToolScreenVo> queryList(ManuPubToolScreenBo bo) {
        LambdaQueryWrapper<ManuPubToolScreen> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ManuPubToolScreen> buildQueryWrapper(ManuPubToolScreenBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ManuPubToolScreen> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ManuPubToolScreen::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), ManuPubToolScreen::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), ManuPubToolScreen::getCategory, bo.getCategory());
        lqw.eq(bo.getFileId() != null, ManuPubToolScreen::getFileId, bo.getFileId());
        return lqw;
    }

    /**
     * 新增车间屏幕管理单
     *
     * @param bo 车间屏幕管理单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ManuPubToolScreenBo bo) {
        ManuPubToolScreen add = MapstructUtils.convert(bo, ManuPubToolScreen.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改车间屏幕管理单
     *
     * @param bo 车间屏幕管理单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ManuPubToolScreenBo bo) {
        ManuPubToolScreen update = MapstructUtils.convert(bo, ManuPubToolScreen.class);
        Long id = bo.getId();

        QosEnum qos = QosEnum.QoS0;
        emqClient.publish("screenUpdate",id.toString(),qos,false);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ManuPubToolScreen entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除车间屏幕管理单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public ScreenVo getScreenByScreenId(Long id) {
        ManuPubToolScreen screen = baseMapper.selectById(id);
        Long ossid = screen.getFileId();
        String url =ossService.getById(ossid).getUrl();
        ScreenVo vo = new ScreenVo();
        BeanUtil.copyProperties(screen, vo);
        vo.setUrl(url);
        return vo;
    }
}
